let api = require('../../../api/api.js')
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tokenState: false,
    display: "none"
  },
  url: function() {
    console.log(url)
  },

  // 最新物流
  logistics: function (e) {
    console.log(e)
    var traces = e.currentTarget.dataset.traces
    console.log(traces)
    this.setData({
      display: "block"
    })
    var data = {}
    data.traces = traces
    api.mylogistics(data, res => {
      console.log(res)
      this.setData({
        title: res.data.result,
        list: res.data.result.list
      })
    })
  },
  // X
  hideview: function () {
    this.setData({
      display: "none"
    })
  },
  // 订单详情
  dingdan:function(e){
    console.log(e)
    var order_sn = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/my/orderContent/orderContent?id=' + order_sn,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  // 优惠劵
  coupon: function(e) {
    wx.navigateTo({
      url: '/pages/my/coupon/coupon',
    })
  },
  // 全部订单
  orderForm: function(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/my/orderForm/orderForm?id=' + id,
    })
  },
  // 邀请好友
  invite: function(e) {
    var myData = this.data.myData
    wx.navigateTo({
      url: '/pages/my/invite/invite?userId=' + myData.user_id,
    })
  },
  // 客服帮助
  phone: function(e) {
    wx.makePhoneCall({
      phoneNumber: '18511175656',
    })
  },
  // 关于我们
  we: function(e) {
    wx.navigateTo({
      url: '/pages/my/we/we',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let token = wx.getStorageSync("token")
    console.log(token)
    let that = this
    if (token) {
      that.setData({
        tokenState: false
      })

      that.my()
      wx.showLoading({})
      api.newuserindex(that.data.options, res => {
        console.log(res)
        that.setData({
          logisticsList: res.data.result.order_list,
          status: res.data.result.order_status,
          info: res.data.result
        })
        wx.hideLoading()
      })
    } else {
      this.setData({
        tokenState: true
      })
    }
   
  },
  //获取个人信息
  my() {
    api.My((res) => {
      console.log(res)

      app.globalData.userInfo = res.data.result.user_img     // 取得全局变量需要的值
      if (res.data.description == 50002) {
        this.setData({
          tokenState: true
        })
      } else {
        console.log(res.data.result)
        wx.setStorageSync('userImg', res.data.result.user_img)
        this.setData({
          myData: res.data.result
        })
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  // 授权
  onGotUserInfo: function(e) {
    var S = this;
    wx.getUserInfo({
      success: function(res) {
        let userInfo = e.detail.userInfo
        let data = {
          user_name: e.detail.userInfo.nickName,
          user_img: e.detail.userInfo.avatarUrl,
          openid_wechat: wx.getStorageSync("key").openid,
          level: app.globalData.level
        }
        console.log(data)
        api.tokens(data, (res) => {
          console.log(res)
          wx.setStorageSync('token', res.data.result)
          api.newuserindex(S.data.options, res => {
            console.log(res)
            S.setData({
              logisticsList: res.data.result.order_list,
              status: res.data.result.order_status,
              info: res.data.result
            })
          })
          S.setData({
            tokenState: false
          })
          S.my()
        })
      },
      fail: function(err) {
        wx.showToast({
          title: '授权失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  }
})